package com.htudata.core.util.operation;

import java.io.File;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileOperationUtils {

  public static String getFileNameSuffix(String fileName) {
    int index = fileName.lastIndexOf(".");
    if (index == -1)
      return ""; 
    String result = fileName.substring(index);
    return result;
  }
  
  public static String getFileNameWithoutSuffix(String fileName) {
    int index = fileName.lastIndexOf(".");
    if (index == -1)
      return ""; 
    String result = fileName.substring(0, index);
    return result;
  }
  
  public static String getFileNameFromFilePath(String filePath, String slash) {
    String result = filePath.substring(filePath.lastIndexOf(slash) + 1);
    return result;
  }
  
  public static String getParentPathFromFilePath(String filePath, String slash) {
    String result = filePath.substring(0, filePath.lastIndexOf(slash));
    return result;
  }
  
  public static Date getFileLastModifiedDateTime(File file) {
    return DateTimeOperationUtils.getDateByTimeInMillis(file.lastModified());
  }
  
  public static List<File> getFileNameList(File dir, String sortType) {
    File[] fileArr = dir.listFiles();
    if (!ArrayOperationUtils.isValidArr((Object[])fileArr))
      return Collections.emptyList(); 
    List<File> result = ArrayOperationUtils.arrToList(fileArr);
    CollectionsOperationUtils.sortByLongField(result, "lastModified", sortType);
    return result;
  }
  
  public static boolean rename(File file, String newFileName) {
    String newFilePath = String.valueOf(file.getParentFile().getAbsolutePath()) + "/" + newFileName;
    return file.renameTo(new File(newFilePath));
  }
  
  public static boolean remove(File file, String newFilePath) {
    return file.renameTo(new File(newFilePath));
  }
  
  public static void deleteDir(File dir, int retainCount) {
    String[] dirNameArr = dir.list();
    if (!ArrayOperationUtils.isValidArr((Object[])dirNameArr))
      return; 
    if (dirNameArr.length < retainCount)
      return; 
    String dirPath = String.valueOf(dir.getAbsolutePath()) + "/";
    int length = dirNameArr.length - retainCount;
    for (int i = 0; i < length; i++) {
      String childDirName = dirNameArr[i];
      String childDirPath = String.valueOf(dirPath) + childDirName;
      deleteDir(new File(childDirPath));
    } 
  }
  
  public static void deleteDir(File dir) {
    String[] childFileNameArr = dir.list();
    String dirPath = String.valueOf(dir.getAbsolutePath()) + "/";
    String childFilePath = "";
    File childFile = null;
    byte b;
    int i;
    String[] arrayOfString1;
    for (i = (arrayOfString1 = childFileNameArr).length, b = 0; b < i; ) {
      String childFileName = arrayOfString1[b];
      childFilePath = String.valueOf(dirPath) + childFileName;
      childFile = new File(childFilePath);
      if (childFile.isDirectory())
        deleteDir(childFile); 
      childFile.delete();
      b++;
    } 
    dir.delete();
  }
  
  public static void deleteSvnDir(File dir) {
    String[] childFileNameArr = dir.list();
    String dirPath = String.valueOf(dir.getAbsolutePath()) + "/";
    String childFilePath = "";
    File childFile = null;
    byte b;
    int i;
    String[] arrayOfString1;
    for (i = (arrayOfString1 = childFileNameArr).length, b = 0; b < i; ) {
      String childFileName = arrayOfString1[b];
      childFilePath = String.valueOf(dirPath) + childFileName;
      childFile = new File(childFilePath);
      if (childFile.isDirectory())
        if (isSvnDir(childFileName)) {
          deleteDir(childFile);
        } else {
          deleteSvnDir(childFile);
        }  
      b++;
    } 
  }
  
  private static String getNewestFile(File dir) {
    if (hasFile(dir))
      return dir.getPath(); 
    String[] childFileNameArr = dir.list();
    if (!ArrayOperationUtils.isValidArr((Object[])childFileNameArr))
      return null; 
    Arrays.sort(childFileNameArr, new Comparator<String>() {
          public int compare(String childFileName1, String childFileName2) {
            int val = childFileName1.compareTo(childFileName2);
            return val;
          }
        });
    String dirPath = String.valueOf(dir.getAbsolutePath()) + "/";
    String firstFilePath = String.valueOf(dirPath) + childFileNameArr[0];
    return getNewestFile(new File(firstFilePath));
  }
  
  public static boolean isSvnDir(String dirName) {
    if (".svn".equals(dirName))
      return true; 
    return false;
  }
  
  public static boolean ofFileType(String fileName, String fileType) {
    if (fileName.endsWith("." + fileType.toLowerCase()))
      return true; 
    if (fileName.endsWith("." + fileType.toUpperCase()))
      return true; 
    return false;
  }
  
  public static boolean hasFile(File file) {
    if (file.exists() && file.isFile())
      return true; 
    return false;
  }
  
  public static boolean hasDir(File dir) {
    if (dir.exists() && dir.isDirectory())
      return true; 
    return false;
  }
  
  public static String tranferFileSize(long size) {
    if (size <= 0L)
      return "0"; 
    String[] units = { "B", "Kb", "Mb", "Gb", "Tb" };
    int digitGroups = (int)(Math.log10(size) / Math.log10(1024.0D));
    return String.valueOf((new DecimalFormat("#,##0.#")).format(size / Math.pow(1024.0D, digitGroups))) + units[digitGroups];
  }
  
  public static Map<String, String> tranferFileSize2(long size) {
    Map<String, String> result = new HashMap<>();
    result.put("units", "");
    result.put("value", "0");
    if (size <= 0L)
      return result; 
    String[] units = { "B", "Kb", "Mb", "Gb", "Tb" };
    int digitGroups = (int)(Math.log10(size) / Math.log10(1024.0D));
    result.put("units", units[digitGroups]);
    result.put("value", (new DecimalFormat("#,##0.#")).format(size / Math.pow(1024.0D, digitGroups)));
    return result;
  }
}
